//
//  UIScrollViewExtension.swift
//  YpfProject
//
//  Created by volantPig on 2021/4/30.
//

import UIKit
import MJRefresh

class UIScrollViewExtension: NSObject {

}

extension UIScrollView {
    
    /// 添加  下拉刷新  功能
    func addRefreshHeader(ignoredTopInset: CGFloat = 0,_ headerCallback : @escaping (() -> Swift.Void )) {
        
        let header = MJRefreshGifHeader.init {
            headerCallback()
        }
        header.ignoredScrollViewContentInsetTop = ignoredTopInset
        header.setTitle("下拉即可刷新...", for: MJRefreshState.idle)
        header.setTitle("释放即可刷新...", for: MJRefreshState.pulling)
        header.setTitle("刷新中...", for: MJRefreshState.refreshing)
        header.lastUpdatedTimeLabel?.isHidden = true
        
        self.mj_header = header
        
    }
    
    /// 添加  上拉加载  功能
    func addRefreshFooter(_ footerCallback : @escaping (() -> Swift.Void )) {
        let footer = MJRefreshBackGifFooter.init {
            footerCallback()
        }
        footer.setTitle("上拉即可加载...", for: MJRefreshState.idle)
        footer.setTitle("释放即可加载...", for: MJRefreshState.pulling)
        footer.setTitle("加载中...", for: MJRefreshState.refreshing)
        footer.setTitle("没有更多数据啦~", for: MJRefreshState.noMoreData)
        self.mj_footer = footer
    }
    
    /// 刷新结束
    func endRefresh() {
        self.mj_header?.endRefreshing()
        self.mj_footer?.endRefreshing()
//        self.ly_endLoading()
    }

    /// 没有更多数据了
    func noMoreData() {
        self.endRefresh()
        self.mj_footer?.endRefreshingWithNoMoreData()
    }
    
}
